﻿using System;
using System.Collections.Generic;
using System.Text;
using SiteServer.Plugin;
using SS.LNDX.Service.Core.Model;
using Dapper;
using System.Data.SqlClient;

namespace SS.LNDX.Service.Core.Provider
{
    public static class EnrollmentDao
    {
        public const string TableName = "ss_lndx_Enrollment";

        public static SqlConnection conn => new SqlConnection(SiteServer.Plugin.Context.ConnectionString);

        public static List<TableColumn> Columns => new List<TableColumn>
        {
            new TableColumn
            {
                AttributeName = nameof(Enrollment.Id),
                DataType = DataType.Integer,
                IsIdentity = true,
                IsPrimaryKey = true
            },
            new TableColumn
            {
                AttributeName = nameof(Enrollment.ContentId),
                DataType = DataType.Integer
            },
            new TableColumn
            {
                AttributeName = nameof(Enrollment.UserId),
                DataType = DataType.Integer
            },
            new TableColumn
            {
                AttributeName = nameof(Enrollment.AddDate),
                DataType = DataType.DateTime
            },
            new TableColumn
            {
                AttributeName = nameof(Enrollment.State),
                DataType = DataType.Integer
            }
        };

        public static int Insert(Enrollment model)
        {
            string query = $"INSERT INTO ss_lndx_Enrollment(ContentId, UserId, State) VALUES(@contentId, @userId, @state);SELECT CAST(SCOPE_IDENTITY() AS INT) AS [Id]";
            return conn.ExecuteScalar<int>(query, new { @contentId = model.ContentId, @userId = model.UserId, @state = model.State });
        }

        public static string Delete(int id)
        {
            string query = $"DELETE FROM ss_lndx_Enrollment WHERE Id={id}";
            Context.DatabaseApi.ExecuteNonQuery(SiteServer.Plugin.Context.ConnectionString, query);
            return "success";
        }

    }

    
}
